package controllers.listeners.semrep;

import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JComboBox;

import objects.Constituent;
import objects.Feature;

import models.FeaturesModel;
import views.MainPanel;

/* Loads the possible values for the selected feature into the value combobox */
/* actionPerformed when you click on the "Add Feature" button and select a feature name */
public class LoadFeatureValuesItemListener implements ItemListener {
	
	private MainPanel view;
	private AddFeatureActionListener listener;
	
	@Override
	public void itemStateChanged(ItemEvent evt) {
		if (evt.getStateChange() == ItemEvent.SELECTED) {
			JComboBox<String> cmbValues = listener.getCmbValues();
			cmbValues.removeAllItems();
			
			for (Constituent currentConstituent : FeaturesModel.getFeatures()) {
				if (currentConstituent.getLabel().equals(view.getCurrentConstituent())) {
					for (Feature currentFeature : currentConstituent.getFeatures().getFeatureList()) {
						if (currentFeature.getName().equalsIgnoreCase(listener.getSelectedName())) {
							for (String currentValue : currentFeature.getValueList()) {
								cmbValues.addItem(currentValue);
							}
						}
					}
				}
			}
		}
	}
	
	public void setView(MainPanel view) {
		this.view = view;
	}
	
	public void setListener(AddFeatureActionListener listener) {
		this.listener = listener;
	}
}
